package com.bonc.apicreator.ctrl;


import java.util.List;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.bonc.apicreator.constant.BaseConstant;
import com.bonc.apicreator.dao.UserDao;
import com.bonc.apicreator.entity.User;
import com.bonc.apicreator.utils.Md5Util;


/**
 * 登录的controller
 *
 * @author gaopengyu
 * @version 2016年8月5日
 * @see LoginController
 * @since
 */
@Controller
public class LoginController {
    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);

    /**
     * userDao
     */
    @Autowired
    private UserDao userDao;

    /**
     * Description: 加载登录界面
     * 
     * @return String
     * @see
     */
    @RequestMapping("/login")
    public String login() {

        return "/login.jsp";

    }

    /**
     * Description: 用户登陆判断
     * 
     * @param username
     * @param password
     * @param session
     * @return String
     * @see
     */
    @RequestMapping(value = {"/log"}, method = RequestMethod.POST)
    public String enter(@RequestParam String username, @RequestParam String password,
                        HttpSession session) {

        String p = Md5Util.kl(Md5Util.md5(password));
        List<User> userlist = userDao.findByUsernameAndPassword(username, p);

        if (userlist.size() != 1) {

            session.setAttribute("message", BaseConstant.STATE_FAILED);
            return "/login.jsp";

        }

        User user = userDao.findByUsername(username);
        session.setAttribute("username", username);
        session.setAttribute("user", user);

        return "redirect:/index";

    }

    /**
     * Description: 返回首页
     * 
     * @return String
     * @see
     */
    @RequestMapping(value = {"/index", "/"}, method = RequestMethod.GET)
    public String index() {
        return "index.jsp";
    }

    /**
     * Description: 登出
     * 
     * @param request
     * @return String
     * @see
     */
    @RequestMapping("/logOut")
    public String logOut(HttpServletRequest request) {
        request.getSession().invalidate();
        return "/login.jsp";
    }

    /**
     * Description: 初始化admin void
     *
     * @see
     */
    @PostConstruct
    public void init() {
        User user = new User();
        user.setUsername("admin");
        user.setPassword(Md5Util.kl(Md5Util.md5("a")));
        user.setRole(1);
        if (StringUtils.isEmpty(userDao.findByUsername(user.getUsername()))) {
            userDao.save(user);
        }
        LOGGER.info("User init success:" + user.toString());

    }

}
